﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmSalesBack : Form
    {
        static string _Storage_SN = "";
        static string _Area_SN = "";
        static string _PartNumber = "";
        static string _DateCode = "";

        bool _IsNG = true;

        public FrmSalesBack()
        {
            InitializeComponent();
        }

        private void FrmSalesBack_Load(object sender, EventArgs e)
        {
            this.txtBillNo.Focus();
        }

        private void txtBillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }
            txtInfo.Text = "";
            InputBillNo();
        }

        public void InputBillNo()
        {
            this.txtMsg.Text = "";
            DataTable info = Bll_ProductOut.GetOutStockInfo(txtBillNo.Text);
            if (info.Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                {
                    RefreshForm(txtBillNo.Text);
                }
                else
                {
                    txtInfo.Text = txtBillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!";
                    txtBillNo.Focus();
                    txtBillNo.Text = "";
                    return;
                }

                if (info.Rows[0]["Status"].ToString() != "0")
                {
                    txtInfo.Text = txtBillNo.Text + "此单号已处理完成,请输入其他的单号!";
                    txtBillNo.Focus();
                    txtBillNo.Text = "";
                    return;
                }

                if (info.Rows[0]["BillType"].ToString() != "销售退货")
                {
                    txtInfo.Text = txtBillNo.Text + "订单类型错误,请输入销售退货类型的单号!";
                    txtBillNo.Focus();
                    txtBillNo.Text = "";
                    return;
                }
            }
            else
            {
                txtInfo.Text = txtBillNo.Text + "此单号不存在,请输入正确的单据!";
                txtBillNo.Focus();
                txtBillNo.Text = "";
                return;
            }

            txtLocation.Focus();
        }

        //刷新画面
        public void RefreshForm(string billNo)
        {
            DataTable info = Bll_ProductOut.GetOutStockInfo(billNo);
            DataTable infoRow = Bll_ProductOut.GetOutStockRowsDT(billNo);
            var msg = "单号:" + info.Rows[0]["BillNo"].ToString() +
              "\r\n仓库:" + info.Rows[0]["Storage_SN"].ToString() +
              "\r\n料号:" + infoRow.Rows[0]["ProductNumber"].ToString() +
              "\r\n退货数量:" + infoRow.Rows[0]["RealQty"].ToString() +
              "/" + infoRow.Rows[0]["PlanQty"].ToString();
            this.txtMsg.Text = msg;

            _Storage_SN = info.Rows[0]["Storage_SN"].ToString();
            _PartNumber = infoRow.Rows[0]["ProductNumber"].ToString();
            _IsNG = Bll_ProductIn.IsNGStorage(_Storage_SN);
        }



        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }
            if (string.IsNullOrEmpty(this.txtBillNo.Text))
            {
                this.txtInfo.Text = "请先选择单号!";
                txtBillNo.Focus();
                return;
            }

            var realLocationSN = txtLocation.Text;

            try
            {
                var snArray = realLocationSN.Split('#');
                var storageSN = snArray[0];

                if (!string.IsNullOrEmpty(_Storage_SN) && !_Storage_SN.Equals(storageSN))
                {
                    this.txtInfo.Text = "入库仓库" + _Storage_SN + "与扫描的仓库" + storageSN + "不一致！";
                    return;
                }

                //txtLocation.Text = snArray[1] + snArray[2];
                _Area_SN = snArray[0] + "#" + snArray[1];

            }
            catch
            {
                this.txtInfo.Text = "库位" + realLocationSN + "格式不正确，请重新输入!";
                txtLocation.Text = "";
                return;
            }

            //不良仓以外
            if (_IsNG == false)
            {
                var InveDT = Bll_ProductIn.GetInventoryInfo(realLocationSN);
                if (InveDT.Rows.Count > 0)
                {
                    if (!_PartNumber.Equals(InveDT.Rows[0]["PartNumber"].ToString()))
                    {
                        this.txtInfo.Text = "入库库位的物料号" + InveDT.Rows[0]["PartNumber"].ToString() + "与库存的物料号" + _PartNumber + "不一致!";
                        txtLocation.Text = "";
                        return;
                    }
                    //保存库存的生产日期
                    _DateCode = InveDT.Rows[0]["DateCode"].ToString();
                }
            }
            //验证库位
            var chkRet = Bll_ProductIn.CheckLocation(realLocationSN);
            if (chkRet == false)
            {
                this.txtInfo.Text = realLocationSN + "库位不存在或不参加分配，请确认!";
                txtLocation.Text = "";
                return;
            }

            this.txtBoxNo.Focus();
        }

        private void txt_BoxNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (string.IsNullOrEmpty(this.txtBillNo.Text))
            {
                this.txtInfo.Text = "请先选择单号!";
                txtBillNo.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtLocation.Text))
            {
                this.txtInfo.Text = "请先输入库位!";
                txtLocation.Focus();
                return;
            }

            var boxNo = this.txtBoxNo.Text;

            //箱号确认
            var dtBox = Bll_ProductIn.GetBoxInfo(boxNo);
            if (dtBox.Rows.Count == 0)
            {
                DialogResult dRet = MessageBox.Show("无此箱号信息,是否继续执行操作", "安全提示", MessageBoxButtons.YesNo,
                                                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dRet == DialogResult.No)
                {
                    txtBoxNo.Text = "";
                    this.txtInfo.Text = "处理终止!";
                    return;
                }

                txtQty.Focus();
            }
            //箱号存在
            else
            {
                if (!_PartNumber.Equals(dtBox.Rows[0]["PartNumber"].ToString()))
                {
                    txtBoxNo.Text = "";
                    this.txtInfo.Text = "箱号的料号【" + dtBox.Rows[0]["PartNumber"].ToString() + "】与库存的料号【" + _PartNumber + "】不一致!";
                    return;
                }
                //非NG仓时
                if (_IsNG == false)
                {
                    if (!_DateCode.Equals(dtBox.Rows[0]["DateCode"].ToString()))
                    {
                        txtBoxNo.Text = "";
                        this.txtInfo.Text = "箱号的DateCode【" + dtBox.Rows[0]["DateCode"].ToString() + "】与库存的DateCode【" + _DateCode + "】不一致!";
                        return;
                    }
                }

                txtQty.Text = dtBox.Rows[0]["PutawayQty"].ToString();
                txtQty.Focus();
            }

            //重复入库check
        }

        private void txt_Qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (string.IsNullOrEmpty(this.txtBillNo.Text))
            {
                this.txtInfo.Text = "请先选择单号!";
                txtBillNo.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtLocation.Text))
            {
                this.txtInfo.Text = "请先输入库位!";
                txtLocation.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxNo.Text))
            {
                this.txtInfo.Text = "请先输入箱号!";
                txtBoxNo.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxNo.Text))
            {
                this.txtInfo.Text = "请先输入箱号!";
                txtBoxNo.Focus();
                return;
            }

            decimal qty = 0;
            try
            {
                qty = Convert.ToDecimal(txtQty.Text);
            }
            catch
            {
                this.txtInfo.Text = "数量格式错误!";
                txtQty.Text = "";
                txtQty.Focus();
                return;
            }

            if (qty == 0)
            {
                this.txtInfo.Text = "数量不能为0!";
                txtQty.Focus();
                return;
            }

            //开始事务
            int code = Bll_ProductIn.SalesBackQuery(this.txtBillNo.Text, Utils.uContext.UserNamek__BackingField,
                                                    _Storage_SN, _Area_SN, this.txtLocation.Text,
                                                    this.txtBoxNo.Text, qty, _IsNG);
            if (code == 1)
            {
                this.txtInfo.Text = "箱号重复!";
                txtBoxNo.Text = "";
                txtBoxNo.Focus();
                return;
            }
            if (code == 2)
            {
                this.txtInfo.Text = "退货数量超过订单数量!";
                txtQty.Focus();
                return;
            }
            RefreshForm(this.txtBillNo.Text);
            this.txtInfo.Text = "";
            this.txtBoxNo.Text = "";
            this.txtQty.Text = "";
            if (code == 9)
            {
                this.txtInfo.Text = "退货完成，订单关闭!";
                this.txtBillNo.Text = "";
                this.txtLocation.Text = "";
            }

        }
        //END
    }
}